use ClientPlatform

function NewGuid() {

        return (S4() + S4() + "-" + S4() + "-4" + S4().substr(0,3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase();

    };

function S4() {

        return (((1+Math.random())*0x10000)|0).toString(16).substring(1); 

    };

function guidToBinData(guid) { 
    guid = guid.replace(/[{}-]/g, "");
    var field1 = guid.substr(0, 8); 
    var field2 = guid.substr(8, 4); 
    var field3 = guid.substr(12, 4); 
    var field4 = guid.substr(16, 4); 
    var field5 = guid.substr(20, 12); 
    field1 = field1.substr(6, 2) + field1.substr(4, 2) + 
field1.substr(2, 2) + field1.substr(0, 2) 
    field2 = field2.substr(2, 2) + field2.substr(0, 2) 
    field3 = field3.substr(2, 2) + field3.substr(0, 2) 
    var hex = field1 + field2 + field3 + field4 + field5 

    // convert hex to base64 
    var base64Digits =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    var base64 = ""; 
    var group; 
    for (var i = 0; i < 30; i += 6) { 
        group = parseInt(hex.substr(i, 6), 16); 
        base64 += base64Digits[(group >> 18) & 0x3f]; 
        base64 += base64Digits[(group >> 12) & 0x3f]; 
        base64 += base64Digits[(group >> 6) & 0x3f]; 
        base64 += base64Digits[group & 0x3f]; 
    } 
    group = parseInt(hex.substr(30, 2), 16); 
    base64 += base64Digits[(group >> 2) & 0x3f]; 
    base64 += base64Digits[(group << 4) & 0x3f]; 
    base64 += "=="; 

    return new BinData(3,base64); 
} 

db.CriteriaLevel2.insert(
    {
        _id: guidToBinData(NewGuid()),
        CriteriaLevel1Id: db.CriteriaLevel1.findOne({ Name: "Products" })._id,
        Name: "Product Involvement",
        UniverseFeature: 9,
        ShortName: "PI"
    });

var templateCriteriaList = [{
    _id: guidToBinData(NewGuid()), Name: "Adult Entertainment", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "AE", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Alcoholic Beverages", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "AL", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Gambling", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "GB", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Pork Products", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "PP", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Tabacco Products", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "TP", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Controversial Weapons", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "CW", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Military Contracting", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "MC", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Non-military Firearms", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "FA", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Animal Testing", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "AT", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Fur and Speciality Leather", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "FL", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Genetically Modified Plants and Seeds", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "GM", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Pesticides", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "PE", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Abortion", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "AB", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Contraceptives", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "CO", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Human Embryonic Stem Cell and Fetal Tissue", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "SC", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Nuclear", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "NP", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Palm Oil", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "PM", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}, {
    _id: guidToBinData(NewGuid()), Name: "Predatory Lending", CriteriaEntity: "PICompanyAssessment", CriteriaEntityField: "Indicators", PIFilterCriteria: null,
    PISymbol: "PL", CriteriaLevel2Id: db.CriteriaLevel2.findOne({ Name: "Product Involvement" })._id, CriteriaType: 4, FilterId: null, CompaniesIdsResult: null
}];

db.TemplateCriteria.insert(templateCriteriaList, { ordered: true });